package com.shunbo.convenient.controller;

import com.shunbo.convenient.service.ProductOrderService;
import com.shunbo.convenient.service.ProductService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.Product;
import com.shunbo.convenient.vo.ProductOrder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author weizhuang
 * 商品购物车
 */
@RestController
@RequestMapping("/productOrder")
public class ProductOrderController {

    @Resource
    private ProductService productService;
    @Resource
    private ProductOrderService productOrderService;

    /**
     * 购物车列表
     *
     * @param productOrder 购物车vo
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getProductList(@RequestBody ProductOrder productOrder) {
        try {

            List<ProductOrder> all = productOrderService.findAll(productOrder);
            for (ProductOrder pro: all
                 ) {
                Product byId = productService.findById(pro.getPid()+"");
                pro.setProduct(byId);
            }
            return ConvenientResult.build(200, "成功", all);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 添加购物车
     *
     * @param productOrder 购物车vo
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveProduct(@RequestBody ProductOrder productOrder) {
        try {
            Integer save = productOrderService.save(productOrder);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 更新购物车
     *
     * @param productOrder 购物车vo
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateProduct(@RequestBody ProductOrder productOrder) {
        try {
            Integer save = productOrderService.update(productOrder);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 删除购物车
     *
     * @param productOrder 购物车vo
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult deleteProduct(@RequestBody ProductOrder productOrder) {
        try {
            Integer save = productOrderService.delete(productOrder.getId() + "");
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }
    /**
     * 删除购物车
     *
     * @param ids 购物车ids
     * @return Result结果集
     */
    @RequestMapping("/deleteIds")
    public ConvenientResult deleteProductIds(String ids) {
        try {
            List<String> idList = Arrays.asList(ids.split(","));
            Integer result  = productOrderService.delByIds(idList);
            return ConvenientResult.build(200, "成功", result);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * id查购物车
     *
     * @param productOrder 购物车vo
     * @return Result结果集
     */
    @RequestMapping("/findOne")
    public ConvenientResult getProductById(@RequestBody ProductOrder productOrder) {
        try {
            ProductOrder prod = productOrderService.findById(productOrder.getId() + "");
            return ConvenientResult.build(200, "成功", prod);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

}
